/*
 * @Author: folusGan 245942153@qq.com
 * @Date: 2024-07-04 14:53:01
 * @LastEditors: folusGan 245942153@qq.com
 * @LastEditTime: 2024-07-11 11:39:15
 * @Description:
 */
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const { execSync } = require('child_process')
// const { execSync } = require('child_process')

const generateFile = path.resolve(__dirname, 'col.json')
const log = {
  info(msg) {
    console.log(chalk.green(msg))
  },
  error(msg) {
    console.log(chalk.red(msg))
  },
  warn(msg) {
    console.log(chalk.yellow(msg))
  },
}
async function readFileSyncText() {
  console.log(chalk.blue('-------------开始读取文件内容-------------'))
  const fileName = path.resolve(__dirname, 'product/col.txt')
  const file = fs.readFileSync(fileName, { encoding: 'utf-8' })
  return file
}
async function writeArrayJson(content) {
  const text = JSON.stringify(content)
  await fs.writeFileSync(generateFile, text, { encoding: 'utf-8' })
}

async function generateTableHeader() {
  const txt = await readFileSyncText()
  let arrTxt = txt.split('\n') || []
  arrTxt.filter((item) => item)
  const jsonContent = arrTxt.map((item) => {
    const tiems = item.split(' ')
    const aferFilter = tiems.filter(Boolean)
    // log.warn(JSON.stringify(aferFilter))
    return {
      introduce: aferFilter[0], //产品介绍
      target: aferFilter[1], //各个类目的id  用它来生成容器
    }
  })
  await writeArrayJson(jsonContent)
}
async function start() {
  await generateTableHeader()
  log.error('-------------生成文件成功,正在格式化-------------')
  execSync(`npx prettier --write ${generateFile}`)
  log.info('-------------格式化成功！-------------')
}
start()
